package com.hippo.core.ui.compose.control

import LoadingDialog
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.hippo.core.R
import com.hippo.core.core.ext.toJson
import com.hippo.core.ui.components.TextPickLayout
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.components.TimeLayout
import com.hippo.core.ui.components.TitleBar
import com.hippo.core.ui.compose.setting.ModeDialog
import com.hippo.core.ui.theme.ColorBackgroundGray
import com.hippo.core.ui.theme.ColorTextBlack
import com.hippo.core.ui.theme.HippoTheme

@Composable
fun CleanScreen(
    onBack: () -> Unit,
    vm: CleanVM = viewModel()
) {

    val uiState = vm.uiState

    val context = LocalContext.current

    var repeatDialog by remember { mutableStateOf(false) }
    var modeDialog by remember { mutableStateOf(false) }

    if (uiState.isLoading) {
        LoadingDialog {
            vm.updateState(
                uiState.copy(
                    isLoading = false
                )
            )
        }
    }

    LaunchedEffect(Unit) {
        vm.getTimer(context)
    }

    if (repeatDialog) {
        RepeatDialog(
            loops = uiState.loops,
            onDismissRequest = {
                repeatDialog = false
            },
            onSure = { loops, days ->
                vm.updateState(
                    uiState.copy(
                        loops = loops,
                        repeat = days,
                    )
                )
            }
        )
    }

    if (modeDialog) {
        ModeDialog(
            currentMode = uiState.mode,
            onDismissRequest = {
                modeDialog = false
            },
            onSave = { mode ->
                vm.updateState(
                    uiState.copy(
                        mode = mode,
                    )
                )
            }
        )
    }


    Column(
        modifier = Modifier
            .background(ColorBackgroundGray)
            .fillMaxSize()
    ) {

        TitleBar(
            title = stringResource(R.string.clean_1),
            onBack = onBack
        )

        Column(
            modifier = Modifier
                .padding(16.dp)
                .verticalScroll(rememberScrollState())
        ) {

            TimeLayout(
                defaultHour = vm.uiState.hour,
                defaultMinute = vm.uiState.minute,
                onHourChange = {
                    vm.updateState(
                        vm.uiState.copy(
                            hour = it,
                        )
                    )
                },
                onMinuteChange = {
                    vm.updateState(
                        vm.uiState.copy(
                            minute = it,
                        )
                    )
                }

            )

            Spacer(modifier = Modifier.height(30.dp))


            Column(
                modifier = Modifier
                    .background(
                        color = Color.White,
                        shape = MaterialTheme.shapes.medium
                    )
            ) {

                TextPickLayout(
                    text = stringResource(R.string.clean_2),
                    value = if(uiState.repeat.isNullOrBlank() || uiState.repeat == "0000000") stringResource(R.string.only) else uiState.repeat,
                    onClick = {
                        repeatDialog = true
                    }
                )

                TextPickLayout(
                    text = stringResource(R.string.clean_5),
                    value = if (uiState.mode == 1) stringResource(R.string.mode_4) else stringResource(
                        R.string.mode_5
                    ),
                    onClick = {
                        modeDialog = true
                    },
                    showLine = false
                )

                HorizontalDivider(thickness = 0.5.dp)

                Column {

                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier
                            .padding(vertical = 6.dp, horizontal = 8.dp)
                    ) {

                        Text(
                            text = stringResource(R.string.clean_4),
                            fontSize = 16.sp,
                            color = ColorTextBlack
                        )

                        Spacer(modifier = Modifier.weight(1f))

                        Switch(
                            checked = uiState.notice,
                            onCheckedChange = { notice ->
                                Log.i("sss", "Switch: $notice")
                                vm.updateState(
                                    uiState.copy(
                                        notice = notice,
                                    )
                                )
                            }
                        )

                    }

                }

            }

        }

        Spacer(modifier = Modifier.height(30.dp))

        ThemeButton(
            text = stringResource(R.string.save),
            modifier = Modifier.padding(16.dp),
            onClick = {
                vm.save {
                    onBack()
                }
            }
        )
    }
}


@Preview(showBackground = true)
@Composable
private fun CleanScreenPreview() {
    HippoTheme {
        CleanScreen(onBack = {})
    }
}